module game {
	/**
	 * 分页滑动容器，继承自egret.ScrollView，要切换的页卡都放在这里
	 * @author caizj
	 *
	 */
	export class SwiperScrollView extends egret.ScrollView{
	
        private scrollGroup: egret.DisplayObjectContainer;
		private touchBegin:boolean;
		private touchMove:boolean;
		private scrollIndex:number = 0;
        private lastTouchPos: number;
        private maxScrollPos: number = 0;
        
        public direction: string = "vertical";//竖版或横版
        
		public constructor() {
            super();
            
            //设置滚动区域宽高
            this.width = AppConfig.WIDTH;
            this.height = AppConfig.HEIGHT;
            //this.bounces = true;//2.4版本启用
            
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
		}
        
       /**
        * 载入舞台
        * 
        */  
		private onAddToStage(event: egret.Event) {
			this.removeEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
			
			//设置滚动内容
			this.scrollGroup = new egret.DisplayObjectContainer();
			this.setContent(this.scrollGroup);
			
			this.initUI();
			
			this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchEvent, this);
			this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEvent, this);
			this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchEvent, this);
            this.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onTouchEvent, this);
            this.addEventListener(egret.TouchEvent.LEAVE_STAGE, this.onTouchEvent, this);
		}
        
        /**
        * 构建需要展示的各个页面
        * 
        */
        private initUI(): void { 
            var swiper: BaseSwiper;
            swiper = new SwiperPage1();
            this.scrollGroup.addChild(swiper);
            
            swiper = new SwiperPage2();
            this.scrollGroup.addChild(swiper);
            
			swiper = new SwiperPage3();
            this.scrollGroup.addChild(swiper);
            
            swiper = new SwiperPage4();
            this.scrollGroup.addChild(swiper);
            
            swiper = new SwiperPage5();
            this.scrollGroup.addChild(swiper);
            
            swiper = new SwiperPage6();
            this.scrollGroup.addChild(swiper);
            
            swiper = new SwiperPage7();
            this.scrollGroup.addChild(swiper);
            
			this.layout();
            this.scrollToIndex(0);
        }
		
	    /**
		 * 布局：竖版或横版
		 * 
		 */
		private layout():void{
			var i:number = 0;
			var num:number = this.scrollGroup.numChildren;
			if(this.direction == "vertical"){
				for(i=0; i<num; i++){
					this.scrollGroup.getChildAt(i).y = i*AppConfig.HEIGHT;
				}
                this.horizontalScrollPolicy = "off";
                this.verticalScrollPolicy = "auto";
                this.maxScrollPos = (num - 1) * AppConfig.HEIGHT;
			}else{
				for(i=0; i<num; i++){
					this.scrollGroup.getChildAt(i).x = i*AppConfig.WIDTH;
				}
                this.verticalScrollPolicy = "off";
                this.horizontalScrollPolicy = "auto";
                this.maxScrollPos = (num - 1) * AppConfig.WIDTH;
			}
		}
        
		/**
        * 滑动操作
        * 
        */
        private onTouchEvent(event:egret.TouchEvent): void { 
            //下载按钮直接跳过
            if(event.target!=null && event.target.name == "download") return;
            
            switch(event.type)
			{
				case egret.TouchEvent.TOUCH_BEGIN:
					this.touchBegin = true;
                    this.touchMove = false;
                    if(this.direction == "vertical"){
                        this.lastTouchPos = this.scrollTop;
                    }else{
                        this.lastTouchPos = this.scrollLeft;
                    }
					break;
				case egret.TouchEvent.TOUCH_MOVE:
					if(this.touchBegin == false) return;
					this.touchMove = true;
					//处理拖动时的区域
                    this.checkAllPageIsActive(true);                   
					break;
				case egret.TouchEvent.TOUCH_END:
                case egret.TouchEvent.LEAVE_STAGE:
                case egret.TouchEvent.TOUCH_RELEASE_OUTSIDE:
					if(this.touchBegin==false ||this.touchMove == false)  return;
                
					//处理释放时要定位的区域.....
					var scrollPos:number = 0;
					if(this.direction == "vertical"){
						scrollPos = this.scrollTop;
                        if(scrollPos < 0 || scrollPos > this.maxScrollPos) return;
                    
                        if(scrollPos > this.lastTouchPos) { //下滑
                            //超过1/3区域，则滑入新页面;否则留在当前页
                            if(scrollPos % AppConfig.HEIGHT >= AppConfig.HEIGHT / 3) {
                                this.throwTo(Math.ceil(scrollPos / AppConfig.HEIGHT) * AppConfig.HEIGHT);
                            } else {
                                this.throwTo(Math.floor(scrollPos / AppConfig.HEIGHT) * AppConfig.HEIGHT);
                            }
                        } else { //上滑
                            //超过1/3区域，则滑入新页面;否则留在当前页
                            if(scrollPos % AppConfig.HEIGHT <= AppConfig.HEIGHT * 2/3 ) {
                                this.throwTo(Math.floor(scrollPos / AppConfig.HEIGHT) * AppConfig.HEIGHT);
                            } else {
                                this.throwTo(Math.ceil(scrollPos / AppConfig.HEIGHT) * AppConfig.HEIGHT);
                            }
                        }
                    
					}else{
						scrollPos = this.scrollLeft;
                        if(scrollPos < 0 || scrollPos > this.maxScrollPos) return;
                    
                        if(scrollPos > this.lastTouchPos) { //右滑
                                //超过1/3区域，则滑入新页面;否则留在当前页
                            if(scrollPos % AppConfig.WIDTH >= AppConfig.WIDTH / 3) {
                                this.throwTo(Math.ceil(scrollPos / AppConfig.WIDTH) * AppConfig.WIDTH);
                            } else {
                                this.throwTo(Math.floor(scrollPos / AppConfig.WIDTH) * AppConfig.WIDTH);
                            }
                        } else { //左滑
                            //超过1/3区域，则滑入新页面;否则留在当前页
                            if(scrollPos % AppConfig.WIDTH <= AppConfig.WIDTH * 2/3 ) {
                                this.throwTo(Math.floor(scrollPos / AppConfig.WIDTH) * AppConfig.WIDTH);
                            } else {
                                this.throwTo(Math.ceil(scrollPos / AppConfig.WIDTH) * AppConfig.WIDTH);
                            }
                        }
					}
					this.touchBegin = this.touchMove = false;
                    event.preventDefault();
                    event.stopImmediatePropagation();
					break;
			}
        }
        
        /**
        * 直接滑动到指定页面
        * 
        */
        public scrollToIndex(index:number): void { 
			this.scrollIndex = index;
            
            if(this.direction == "vertical"){
                this.setScrollPosition(index*AppConfig.HEIGHT,0,false);
            }else{
                this.setScrollPosition(0,index*AppConfig.WIDTH, false);
            }
            this.checkAllPageIsActive(false);
        }
        
        /**
        * 快速滑动到目标位置
        * @ --300毫秒
        */
        public throwTo(pos:number): void { 
            if(this.direction == "vertical"){
                //this.setScrollPosition(pos,0,false);
                egret.Tween.get(this).to({ "scrollTop": pos , "scrollLeft":0},300, egret.Ease.backOut).call(this.onThrowComplete, this);
            }else{
//                this.setScrollPosition(0,pos,false);
                egret.Tween.get(this).to({ "scrollLeft": pos , "scrollTop": 0},300,egret.Ease.backOut).call(this.onThrowComplete, this);
            }
        }
        
        /**
        * 缓动特效结束:更新页面索引值
        * 
        */
        private onThrowComplete(): void { 
            egret.Tween.removeTweens(this);
            
            if(this.direction == "vertical") {
                this.scrollIndex = Math.floor(this.scrollTop / AppConfig.HEIGHT);
            } else { 
                this.scrollIndex = Math.floor(this.scrollLeft / AppConfig.WIDTH);
            }
            this.checkAllPageIsActive(false);
//            console.log("scrollIndex,,"+this.scrollIndex);
        }
        
        /**
        * 滑动或切换成功后，设置处于焦点的页面为激活，其他为静止
        * @optimize --是否启用性能优化，避免过度设置
        */
        private checkAllPageIsActive(optimize:boolean): void { 
            var childNum:number = this.scrollGroup.numChildren;
            var i: number = 0;
            var swiper: BaseSwiper;
            for(i = 0;i < childNum;i++) { 
                swiper = <BaseSwiper> this.scrollGroup.getChildAt(i);
                
                //判断是否启用性能优化
                if(optimize) {
                    
                    //间隔两层不处理
                    if(Math.abs(i - this.scrollIndex) >= 2) continue;

                    //当前页启用激活状态，其他页为静止状态
                    if(i == this.scrollIndex) {
                        swiper.activate();
                    } else {
                        swiper.deactivate();
                    }
                } else { 
                    
                    //当前页启用激活状态，其他页为静止状态
                    if(i == this.scrollIndex) {
                        swiper.activate();
                    } else {
                        swiper.deactivate();
                    }
                }
            }
        }
	}
}
